package com.dcloud.flow.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dcloud.flow.entity.AipFlowNode;
import com.dcloud.flow.entity.request.GetFlowByTaskIdRequest;
import com.dcloud.flow.entity.request.SaveNodeFieldMapRequest;
import com.dcloud.flow.entity.request.SaveNodeValueRequest;
import com.dcloud.flow.entity.vo.NodeFieldMap;
import com.dcloud.flow.mapper.xml.NodeVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.dcloud.flow.entity.AipFlow;
import com.dcloud.flow.service.IAipFlowService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: aip_flow
 * @Author: jeecg-boot
 * @Date:   2023-07-28
 * @Version: V1.0
 */
@Api(tags="aip_flow")
@RestController
@RequestMapping("/api/aipflow/v1")
@Slf4j
public class AipFlowController extends JeecgController<AipFlow, IAipFlowService> {
	@Autowired
	private IAipFlowService aipFlowService;


	 /**
	  * 根据任务id获取流程信息
	  *
	  * @return
	  */
	 @AutoLog(value = "根据任务id获取流程信息")
	 @ApiOperation(value="根据任务id获取流程信息", notes="根据任务id获取流程信息")
	 @PostMapping(value = "/getFlowByTaskId")
	 public Result<?> tree(@RequestBody GetFlowByTaskIdRequest getFlowByTaskIdRequest) {
		 if(getFlowByTaskIdRequest.getTaskId() == null){
			 return Result.error("未正确传参");
		 }
		 AipFlow flowByTaskId = aipFlowService.getFlowByTaskId(getFlowByTaskIdRequest);
		 return Result.OK(flowByTaskId);
	 }


	 /**
	  * 修改流程配置信息
	  *
	  * @return
	  */
	 @AutoLog(value = "修改流程配置信息")
	 @ApiOperation(value="修改流程配置信息", notes="修改流程配置信息")
	 @PostMapping(value = "/updateById")
	 public Result<?> updateById(@RequestBody AipFlow aipFlow) {
		 boolean b = aipFlowService.updateById(aipFlow);
		 return Result.OK(b);
	 }


	 /**
	  * 流程节点配置参数保存
	  *
	  * @return
	  */
	 @AutoLog(value = "流程节点配置参数保存")
	 @ApiOperation(value="流程节点配置参数保存", notes="流程节点配置参数保存")
	 @PostMapping(value = "/node/save")
	 public Result<?> saveNode(@RequestBody SaveNodeValueRequest saveNodeValueRequest) {
		 aipFlowService.saveNode(saveNodeValueRequest);
		 return Result.OK("");
	 }


	 /**
	  * 根据节点id查询节点配置信息
	  *
	  * @return
	  */
	 @AutoLog(value = "根据节点id查询节点配置信息")
	 @ApiOperation(value="根据节点id查询节点配置信息", notes="根据节点id查询节点配置信息")
	 @PostMapping(value = "/node/info")
	 public Result<?> getNodeByNodeId(@RequestBody SaveNodeValueRequest saveNodeValueRequest) {
		 AipFlowNode aipFlowNode = aipFlowService.getNodeByNodeId(saveNodeValueRequest);
		 return Result.OK(aipFlowNode);
	 }

	 /**
	  * 根据节点id自动生成出入参数映射
	  *
	  * @return
	  */
	 @AutoLog(value = "根据节点id自动生成出入参数映射")
	 @ApiOperation(value="根据节点id自动生成出入参数映射", notes="根据节点id自动生成出入参数映射")
	 @PostMapping(value = "/node/gen/param")
	 public Result<?> genNodeParam(@RequestBody SaveNodeValueRequest saveNodeValueRequest) {
		 try {
			 List<NodeFieldMap> nodeFieldMaps = aipFlowService.genNodeParam(saveNodeValueRequest);
			 return Result.OK(nodeFieldMaps);
		 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e  ) {
			 log.error("根据节点id自动生成出入参数映射失败,{}",e.getMessage());
			 return Result.error("根据节点id自动生成出入参数映射,{}",e.getMessage());
		 }

	 }

	 /**
	  * 保存节点的字段映射
	  *
	  * @return
	  */
	 @AutoLog(value = "保存节点的字段映射")
	 @ApiOperation(value="保存节点的字段映射", notes="保存节点的字段映射")
	 @PostMapping(value = "/node/fieldMap/save")
	 public Result<?> saveNodeFieldMap(@RequestBody SaveNodeFieldMapRequest saveNodeFieldMapRequest) {
		 try {
			 aipFlowService.saveNodeFieldMap(saveNodeFieldMapRequest);
			 return Result.OK(true);
		 } catch (JsonProcessingException e) {
			 log.error("保存节点的字段映射失败,{}",e.getMessage());
			 return Result.error("保存节点的字段映射失败,{}",e.getMessage());
		 }

	 }

}
